$(() => {
  /*=== bannerStart ===*/
  let $banner = $('.banner');
  let $picLi = $(".banner .pic li");
  let $prev = $('.banner .prev');
  let $next = $('.banner .next');
  let $tabLi = $('.banner .tab li');
  let index = 0;
  // 小原点
  $tabLi.click(function () {
    index = $(this).index();
    $(this).addClass('active').siblings('li').removeClass('active');
    $picLi.eq(index).fadeIn(1500).siblings('li').fadeOut(1500);
  });
  // 点击切换上一张
  $prev.click(() => {
    index--;
    if (index < 0) {
      index = $tabLi.length - 1
    }
    $tabLi.eq(index).addClass('active').siblings('li').removeClass('active');
    $picLi.eq(index).fadeIn(1500).siblings('li').fadeOut(1500);
  }).mousedown(() => false);
  // 点击切换下一张
  $next.click(() => {
    auto();
  }).mousedown(() => false);

  //  图片向前滑动
  function auto() {
    index++;
    index %= $tabLi.length;
    $tabLi.eq(index).addClass('active').siblings('li').removeClass('active');
    $picLi.eq(index).fadeIn(1500).siblings('li').fadeOut(1500);
  }

  // 定时器
  let timer = setInterval(auto, 2500);
  $banner.hover(() => {
    clearInterval(timer)
  }, () => {
    auto()
  });
  /*=== bannerEnd ===*/
  let $content = $(".content");
  /*=== load more =====*/
  let $moreBtn = $(".btn-more");
  let $newsContain = $(".news-contain");
  let $newsList = $(".news-list");
  console.log($moreBtn);
  // 原生JS jq对象转为js对象 才能用
  $moreBtn[0].addEventListener('click', function () {
  // $moreBtn.click(function () {
  //   alert(1);
    $newsContain.append(`<div class="loading-img"></div>`);
    let $loadImg = $(".loading-img");
    let tagId = $('.news-nav ul li.active').children('a').data("id");
    let page= $(this).data("page");
    console.log(`tagId ${tagId}`);
    console.log(`page ${page}`);
    // ajax
   // setTimeout(()=>{
      $.get({
      url: "/news/list/",
      data: {
        "page": page,
        "tag_id": tagId,
      },
      success: res => {
        if (res["code"] === 0) {
          let data = res["data"];
          let newses = data["newses"];
          // console.log(newses);  null '' false undefined 0
          if (newses.length > 0) {
            newses.forEach((news) => {
                  console.log(news);
              let pub_time = news["pub_time"];
              let result = dateFormat(pub_time);
              // `` 模板字符串 ${news.title}
              let newsStr = `
                <li class="news-item">
                  <a href="/news/detail/${news.id}" class="news-thumbnail" target="_blank">
                    <img src="${news.thumbnail_url}" alt="title" title="${news.title}">
                  </a>
                  <div class="news-content">
                    <h4 class="news-title"><a href="/news/detail/${news.id}">${news.title}</a></h4>
                    <p class="news-details">${news.desc}</p>
                    <div class="news-other">
                      <span class="news-type">${news.tag.name}</span>
                      <span class="news-time">${result}</span>
                      <span class="news-author">${news.author.username}</span>
                    </div>
                  </div>
                </li>
              `;
              $newsList.append(newsStr);
            })
          } else {
            $(this).remove();
          }
          $loadImg.remove();
        }
        page ++; // page
        // attr(“data-page”, 2) data("page") 统一规范
        $(this).data("page", page);
      },
      error: err => {
        logErr(err);
      }
    })
   // }, 200)
  });

  /*=== newsNavStart ===*/
  let $newsLi = $('.news-nav ul li');
  $newsLi.click(function (event) {
    $(this).addClass('active').siblings('li').removeClass('active');
    let tagId = $(this).children('a').data('id');
    $moreBtn.data("page", 2);
    $.get({
      url: "/news/list/",
      data: {
        "tag_id": tagId,
      },
      success: res => {
        if (res["code"] === 0) {
          let data = res["data"];
          let newses = data["newses"];
          $newsList.children().remove();
          if (newses.length > 0) {
            newses.forEach(news => {
              let pub_time = news["pub_time"];
              let result = dateFormat(pub_time);
              // `` 模板字符串 ${news.title}
              let newsStr = `
                <li class="news-item">
                  <a href="/news/detail/${news.id}" class="news-thumbnail" target="_blank">
                    <img src="${news.thumbnail_url}" alt="title" title="${news.title}">
                  </a>
                  <div class="news-content">
                    <h4 class="news-title"><a href="/news/detail/${news.id}">${news.title}</a></h4>
                    <p class="news-details">${news.desc}</p>
                    <div class="news-other">
                      <span class="news-type">${news.tag.name}</span>
                      <span class="news-time">${result}</span>
                      <span class="news-author">${news.author.username}</span>
                    </div>
                  </div>
                </li>
              `;
              $newsList.append(newsStr);
              $content.append($moreBtn);
            })
          } else {
            $moreBtn.remove();
          }
        }
      }
    })
  });
  /*=== newsNavEnd ===*/
});

